package com.springboot.mybatis.mapper;

import com.springboot.mybatis.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

//@Mapper
public interface UserMapper {
    @Select("select * from t_users where id = #{id}")
    User selectUserById(Integer id);

    @Select("select id,name,password,sex,birthday,registTime " +
            "from t_users where id = #{id} and password = #{pwd}")
    User selectUserByIdAndPwd(Integer id , String pwd);

    @Select("select id,name,password,sex,birthday,registTime " +
            "from t_users where id = #{id} and password = #{pwd}")
    User selectUserByIdAndPwd3(@Param("id") Integer id , @Param("pwd") String pwd);

    //使用对象属性进行参数绑定 用的最多
    @Select("SELECT id,name,password,sex,birthday,registTime\n" +
            "       FROM t_users\n" +
            "       WHERE id = #{myId}\n" +
            "         AND password = #{myPwd}")
    User selectUserByIdAndPwd4(Map values);

    @Select("SELECT id,name,password,sex,birthday,registTime\n" +
            "       FROM t_users\n" +
            "       WHERE id = #{id}\n" +
            "         AND password = #{password}")
    User selectUserByUserInfo(User user);

    @Select("SELECT id,name,password,sex,birthday,registTime FROM t_users WHERE name like concat('%','${keyword}','%')")
    List<User> selectUsersByKeyword(String keyword);

    @Select("SELECT id,name,password,sex,birthday,registTime\n" +
            "        FROM t_users\n" +
            "        WHERE name LIKE '%${keyword}%'")
    List<User> selectUsersByKeyword2(String keyword);

    List<User> selectUsersByKeyword3(String keyword);

    @Select("select * from t_users")
    List<User> selectAllUsers();

    @Delete("delete from t_users where id = #{id}")
    void deleteUserById(Integer id);

    @Update("update t_users set name = #{name},password = #{password}, " +
            "sex = #{sex},birthday = #{birthday}," +
            "registTime = #{registTime} where id = #{id}" )
    void updateUserById(User user);

    @Insert("insert into t_users values (#{id},#{name},#{password},#{sex},#{birthday},#{registTime})")
    void insertUser(User user);
}